package com.jdd.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.dao.entity.*;
import com.jdd.dao.mapper.*;
import com.jdd.dao.service.*;
import com.jdd.modules.people.model.TProjectTreeModel;
import com.jdd.modules.system.util.FindsTprojectChildrenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-23
 */
@Service
public class TProjectServiceImpl extends ServiceImpl<TProjectMapper, TProject> implements TProjectService {
    @Resource
    private TProjectMapper tProjectMapper;
    @Resource
    private TAreaMapper tAreaMapper;
    @Resource
    private TBuildingMapper tBuildingMapper;
    @Resource
    private TUnitMapper tUnitMapper;
    @Resource
    private ITUnitService tUnitService;

    @Resource
    private ITFloorService itFloorService;

    @Resource
    private ITHouseService itHouseService;

    @Resource
    private TFloorMapper tFloorMapper;

    @Resource
    private ITBuildingService tBuildingService;


    @Autowired
    private FindsTprojectChildrenUtil findsTprojectChildrenUtil;
    @Override
    public void bind(ArrayList<String> projectIds, String parentId,String areaName,String areaId) {
        /*项目绑定好区域*/
        for (String projectId : projectIds) {
            tProjectMapper.bind(projectId,areaId,areaName);
//            tProjectMapper.bindName(projectId,areaName);
        }
        /*判断是新生成区域还是用的老区域*/
//        TArea tArea = tAreaMapper.selectById(areaId);
//        if (tArea.getId().equals(parentId)){
//            tArea.setParentId(parentId);
//        }
//        tAreaMapper.updateById(tArea);
    }

    @Override
    public List<TProjectTreeModel> queryTreeList() {
        LambdaQueryWrapper<TProject> query = new LambdaQueryWrapper<>();
        List<TProject> list;
        List<TArea> areaList = new ArrayList<>();
        List<TBuilding> tBuildingList = new ArrayList<>();
        List<TUnit> tUnits = new ArrayList<>();
        List<TFloor> tFloors = new ArrayList<>();
        list = this.list(query);
        for(TProject project:list)
        {
           List<TArea> child = tAreaMapper.selectByProjectId(project.getId());
           if (child.size()>0){
               areaList.addAll(child);
               QueryWrapper<TBuilding> tBuildingQueryWrapper = new QueryWrapper<>();
               tBuildingQueryWrapper.lambda().in(TBuilding::getAreaId,child.stream().map(TArea::getId).collect(Collectors.toList()));
               List<TBuilding> buildingChild = tBuildingService.list(tBuildingQueryWrapper);
               tBuildingList.addAll(buildingChild);
               if (buildingChild.size()>0){
                   LambdaQueryWrapper<TUnit> tUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
                   tUnitLambdaQueryWrapper.in(TUnit::getBuildingId,buildingChild.stream().map(TBuilding::getId).collect(Collectors.toList()));
                   List<TUnit> unitChild = tUnitService.list(tUnitLambdaQueryWrapper);
                   tUnits.addAll(unitChild);
                   if (unitChild.size()>0){
                       LambdaQueryWrapper<TFloor> tFloorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                       tFloorLambdaQueryWrapper.in(TFloor::getUnitId,unitChild.stream().map(TUnit::getId).collect(Collectors.toList()));
                       List<TFloor> floorChild = itFloorService.list(tFloorLambdaQueryWrapper);
                       tFloors.addAll(floorChild);
                   }


               }

           }

        }
        return findsTprojectChildrenUtil.wrapTreeDataToTreeList(list,areaList,tBuildingList,tUnits,tFloors);
    }



    @Override
    public List<TProject> selectByAreaId(String parentId) {
        List<TProject> tProjects = tProjectMapper.selectByAreaId(parentId);
        return tProjects;
    }

    @Override
    public String getOrgCodeLastCreateProject() {
        return tProjectMapper.getOrgCodeLastCreateProject();
    }

    @Override
    public boolean removeBind(String id) {
        return tProjectMapper.removeBind(id);
    }

}
